#include "LoginDialog.h"
#include "ui_LoginDialog.h"
#include <QMessageBox>
#include <QPixmap>
#include <QPalette>
#include <QBrush>
#include <QPainter>

LoginDialog::LoginDialog(System* sys, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    //this->setStyleSheet("MainWindow { background-image: url(2.png); }");

    this->sys = sys;

    //设置密码框lineEdit2中的内容不可见
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    ui->lineEdit_4->setEchoMode(QLineEdit::Password);
    ui->lineEdit_12->setEchoMode(QLineEdit::Password);

    initUi();
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

//登录按钮
void LoginDialog::on_pushButton_clicked()
{
    //格式化lineEdit的文本
    QString account = ui->lineEdit->text();
    //格式化lineEdit_2的文本
    QString password = ui->lineEdit_2->text();
    QSqlQuery query(sys->db);
    query.prepare("SELECT * FROM account WHERE account = ? AND password = ?");
    query.bindValue(0, account);
    query.bindValue(1, password);
    if (query.exec()) {
        if (query.next()) {
            sys->account = account;
            sys->password = password;
            sys->email = query.value("email").toString();
            sys->name1 = query.value("name1").toString();
            sys->id1 = query.value("id1").toString();
            sys->phone1 = query.value("phone1").toString();
            sys->name2 = query.value("name2").toString();
            sys->id2 = query.value("id2").toString();
            sys->phone2 = query.value("phone2").toString();

            emit loginSuccessful();
        } else {
            // 结果集为空，登录失败
            QMessageBox::critical(this, "tip", "请输入正确的账号密码");
        }
    } else {
        // 执行查询失败
        QMessageBox::critical(this, "tip", "执行查询失败");
    }
}

//取消登录按钮
void LoginDialog::on_pushButton_2_clicked()
{
    close();
}

//单名账号注册按钮
void LoginDialog::on_pushButton_3_clicked()
{
    //格式化lineEdit3的文本作为account
    QString account = ui->lineEdit_3->text();
    //格式化lineEdit4的文本作为password
    QString password = ui->lineEdit_4->text();
    //格式化lineEdit5的文本作为email
    QString email = ui->lineEdit_5->text();
    //格式化lineEdit6的文本作为name1
    QString name1 = ui->lineEdit_6->text();
    //格式化lineEdit7的文本作为id1
    QString id1 = ui->lineEdit_7->text();
    //格式化lineEdit8的文本作为phone1
    QString phone1 = ui->lineEdit_8->text();

    //如果发现任意一个字符串为空，提示用户
    if (account.isEmpty() || password.isEmpty() || email.isEmpty() || name1.isEmpty() || id1.isEmpty() || phone1.isEmpty()) {
        QMessageBox::critical(this, "tip", "请输入完整信息");
        return;
    }

    //调用系统类的注册函数
    sys->registerAccount1(account, password, email, name1, id1, phone1);
    QMessageBox::information(this, "tip", "注册成功！");
}

//联名账号注册按钮
void LoginDialog::on_pushButton_4_clicked()
{
    //格式化lineEdit9的文本作为account
    QString account = ui->lineEdit_9->text();
    //格式化lineEdit12的文本作为password
    QString password = ui->lineEdit_12->text();
    //格式化lineEdit13的文本作为email
    QString email = ui->lineEdit_13->text();
    //格式化lineEdit18的文本作为name1
    QString name1 = ui->lineEdit_18->text();
    //格式化lineEdit19的文本作为id1
    QString id1 = ui->lineEdit_19->text();
    //格式化lineEdit20的文本作为phone1
    QString phone1 = ui->lineEdit_20->text();
    //格式化lineEdit21的文本作为name2
    QString name2 = ui->lineEdit_21->text();
    //格式化lineEdit22的文本作为id2
    QString id2 = ui->lineEdit_22->text();
    //格式化lineEdit23的文本作为phone2
    QString phone2 = ui->lineEdit_23->text();

    //如果发现任意一个字符串为空，提示用户
    if (account.isEmpty() || password.isEmpty() || email.isEmpty() || name1.isEmpty() || id1.isEmpty() || phone1.isEmpty() || name2.isEmpty() || id2.isEmpty() || phone2.isEmpty()) {
        QMessageBox::critical(this, "tip", "请输入完整信息");
        return;
    }

    //调用系统类的注册函数
    sys->registerAccount2(account, password, email, name1, id1, phone1, name2, id2, phone2);
    QMessageBox::information(this, "tip", "注册成功！");
}

void LoginDialog::initUi()
{
    //ui->tabWidget->setStyleSheet("background-color: rgba(255, 255, 255, 127);");
    //ui->tabWidget->setStyleSheet("QTabWidget:pane{border: 1px solid black; top: -1px;background: transparent;}QTabBar::tab{height:30px; margin-right: 2px; margin-bottom:-2px;}");
    ui->tabWidget->setStyleSheet(
        "QTabWidget::pane {"
        "   border: 1px solid black;"
        "   top: -1px;"
        "   background: rgba(255, 255, 255, 127);" // 50% transparent white
        "}"
        "QTabBar::tab {"
        "   height: 30px;"
        "   margin-right: 2px;"
        "   margin-bottom: -2px;"
        "   background: rgba(255, 255, 255, 127);" // You can also make the tabs semi-transparent
        "}"
        );


    ui->lineEdit->setStyleSheet("QLineEdit {"
                                "background-color: rgba(255, 255, 255, 111);"
                                "}");
    ui->lineEdit_2->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");

    ui->lineEdit_12->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_13->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_18->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_19->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_20->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_21->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_22->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_23->setStyleSheet("QLineEdit {"
                                   "background-color: rgba(255, 255, 255, 111);"
                                   "}");
    ui->lineEdit_3->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->lineEdit_4->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->lineEdit_5->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->lineEdit_6->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->lineEdit_7->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->lineEdit_8->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->lineEdit_9->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");

    ui->pushButton->setStyleSheet("QPushButton {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->pushButton_2->setStyleSheet("QPushButton {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->pushButton_3->setStyleSheet("QPushButton {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");
    ui->pushButton_4->setStyleSheet("QPushButton {"
                                  "background-color: rgba(255, 255, 255, 111);"
                                  "}");


}
